package com.fish.service.impl;

import java.util.Date;

import com.fish.account.dao.AccountDAO;
import com.fish.account.dao.BalanceDAO;
import com.fish.account.po.Account;
import com.fish.account.po.Balance;
import com.fish.common.constant.Constant;
import com.fish.order.dao.OrderDAO;
import com.fish.order.po.Order;
import com.fish.service.BusinessLogicService;
import com.fish.utils.UUIDGenerator;

/**
 * 业务逻辑处理服务
 * 
 * @author clj
 * 
 */
public class BusinessLogicServiceImpl implements BusinessLogicService {
	private AccountDAO accountDAO;
	private BalanceDAO balanceDAO;
	private OrderDAO orderDAO;

	@Override
	public synchronized void applyCodeSuccessLogic(Order order) {
		// 1.操作用户的存款账户，扣除二维码申请费用,到平台
		Balance userBalance = this.balanceDAO.queryBalanceByUser(order.getUserId());
		Balance adminBalance = this.balanceDAO.queryBalanceByUser(Constant.ADMIN_USER);
		Double currentUserBalance = userBalance.getBalance() - Constant.PAY_CODE_AMOUNT;
		Double currentAdminBalance = adminBalance.getBalance() + Constant.PAY_CODE_AMOUNT;
		// 操作旅行社用户
		Account userAccount = new Account();
		userAccount.setAccountId(UUIDGenerator.getNewUUID());
		userAccount.setAmount(-Constant.PAY_CODE_AMOUNT);
		userAccount.setBalance(currentUserBalance);
		userAccount.setContent("扣除二维码申请费用,到平台");
		userAccount.setOperateTime(new Date());
		userAccount.setType("PAY_CODE");
		userAccount.setUserId(order.getUserId());
		this.accountDAO.insertAccount(userAccount);
		// 操作平台用户
		Account adminAccount = new Account();
		adminAccount.setAccountId(UUIDGenerator.getNewUUID());
		adminAccount.setAmount(Constant.PAY_CODE_AMOUNT);
		adminAccount.setBalance(currentAdminBalance);
		adminAccount.setContent("二维码申请费用,到平台");
		adminAccount.setOperateTime(new Date());
		adminAccount.setType("PAY_CODE");
		adminAccount.setUserId(Constant.ADMIN_USER);
		this.accountDAO.insertAccount(adminAccount);
		// 旅行社用户
		userBalance.setLastBalance(userBalance.getBalance());
		userBalance.setBalance(currentUserBalance);
		userBalance.setUserId(order.getUserId());
		this.balanceDAO.updateByUser(userBalance);
		// 平台用户
		adminBalance.setLastBalance(adminBalance.getBalance());
		adminBalance.setBalance(currentAdminBalance);
		adminBalance.setUserId(Constant.ADMIN_USER);
		this.balanceDAO.updateByUser(adminBalance);
	}

	@Override
	public synchronized void orderFerformedLogic(Order order) {
		// 1.更新订单履行状态，设置为已履行
		// 2.操作用户的存款账户，扣除二维码刷码费用，到平台
		Order newOrder = new Order();
		newOrder.setPerformStatus("true");
		newOrder.setOrderId(order.getOrderId());
		newOrder.setUpdateTime(new Date());
		this.orderDAO.updateOrder(order);
		
		Balance userBalance = this.balanceDAO.queryBalanceByUser(order.getUserId());
		Balance adminBalance = this.balanceDAO.queryBalanceByUser(Constant.ADMIN_USER);
		Double currentUserBalance = userBalance.getBalance() - Constant.PAY_USE_CODE_AMOUNT;
		Double currentAdminBalance = adminBalance.getBalance() - Constant.PAY_USE_CODE_AMOUNT;
		// 操作旅行社用户
		Account userAccount = new Account();
		userAccount.setAccountId(UUIDGenerator.getNewUUID());
		userAccount.setAmount(-Constant.PAY_USE_CODE_AMOUNT);
		userAccount.setBalance(currentUserBalance);
		userAccount.setContent("扣除二维码刷码费用，到平台");
		userAccount.setOperateTime(new Date());
		userAccount.setType("PAY_CODE");
		userAccount.setUserId(order.getUserId());
		this.accountDAO.insertAccount(userAccount);
		// 操作平台用户
		Account adminAccount = new Account();
		adminAccount.setAccountId(UUIDGenerator.getNewUUID());
		adminAccount.setAmount(Constant.PAY_USE_CODE_AMOUNT);
		adminAccount.setBalance(currentAdminBalance);
		adminAccount.setContent("二维码刷码费用,到平台");
		adminAccount.setOperateTime(new Date());
		adminAccount.setType("PAY_CODE");
		adminAccount.setUserId(Constant.ADMIN_USER);
		this.accountDAO.insertAccount(adminAccount);
		// 旅行社用户
		userBalance.setLastBalance(userBalance.getBalance());
		userBalance.setBalance(currentUserBalance);
		userBalance.setUserId(order.getUserId());
		this.balanceDAO.updateByUser(userBalance);
		// 平台用户
		adminBalance.setLastBalance(adminBalance.getBalance());
		adminBalance.setBalance(currentAdminBalance);
		adminBalance.setUserId(Constant.ADMIN_USER);
		this.balanceDAO.updateByUser(adminBalance);

	}

	public void setAccountDAO(AccountDAO accountDAO) {
		this.accountDAO = accountDAO;
	}

	public void setBalanceDAO(BalanceDAO balanceDAO) {
		this.balanceDAO = balanceDAO;
	}

	public void setOrderDAO(OrderDAO orderDAO) {
		this.orderDAO = orderDAO;
	}

}
